package org.naxx.security.model.impl.match;

import org.naxx.security.expression.Node;
import org.naxx.security.expression.Node.NodeType;
import org.naxx.security.expression.impl.NodeImpl;
import org.naxx.security.function.FunctionType;
import org.naxx.security.model.match.AttributeDesignator;
import org.naxx.security.model.match.Match;
import org.naxx.security.model.match.ValueHolder;

public class MatchImpl implements Match {
	
	private AttributeDesignator attributeDesignator;
	private ValueHolder valueHolder;
	private FunctionType functionType;

	public MatchImpl() {
	}

	public MatchImpl(AttributeDesignator attributeDesignator, ValueHolder valueHolder, 
			FunctionType functionType) {
		this.attributeDesignator = attributeDesignator;
		this.valueHolder = valueHolder;
		this.functionType = functionType;
	}

	public AttributeDesignator getAttributeDesignator() {
		return this.attributeDesignator;
	}

	public ValueHolder getValueHolder() {
		return this.valueHolder;
	}

	public FunctionType getFunctionType() {
		return this.functionType;
	}

	public void setAttributeDesignator(AttributeDesignator attributeDesignator) {
		this.attributeDesignator = attributeDesignator;
	}

	public void setValueHolder(ValueHolder valueHolder) {
		this.valueHolder = valueHolder;
	}

	public void setFunctionType(FunctionType functionType) {
		this.functionType = functionType;
	}
	
	public Node asTree() {
		NodeImpl root = new NodeImpl();

		NodeImpl right = new NodeImpl();
		right.setAttributeDesignator(this.attributeDesignator);
		right.setNodeType(NodeType.LEAF);
		right.setParent(root);
		
		NodeImpl left = new NodeImpl();
		left.setValueHolder(this.valueHolder);
		left.setNodeType(NodeType.LEAF);
		left.setParent(root);
		left.setSibiling(right);
		
		root.setFunctionType(this.functionType);
		root.setNodeType(NodeType.NON_LEAF);
		root.addChild(left);
		root.addChild(right);
		return root;
	}
}
